﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PillerPool : MonoBehaviour {

	public GameObject pillar;
	private Vector2 initPosition = new Vector2(-15, -25);
	private GameObject[] pillars;
	private int pillarPoolSize = 5;
	private float spwan = 3f;
	private float initSpwan = 0f;
	private int pillarsIndex = 0;

	void Start() {
		pillars = new GameObject[pillarPoolSize];
		for (int i = 0; i < pillarPoolSize; i++) {
			pillars[i] = Instantiate(pillar, initPosition, Quaternion.identity);
		}
		showPillar();
	}

	// Update is called once per frame
	void Update() {
		if (GameController.initialize.gameIsOver) {
			stop();
			return;
		}
		initSpwan += Time.deltaTime;
		if (initSpwan >= spwan) {
			initSpwan = 0;
			showPillar();
		}
	}

	private void showPillar() {
		float rate = Random.Range(-1.4f, 3.4f);
		pillars[pillarsIndex].transform.position = new Vector2(0, rate);
		pillarsIndex++;
		if (pillarsIndex >= pillarPoolSize) {
			pillarsIndex = 0;
		}
	}

	//停止移动
	public void stop() {
		for (int i = 0; i < pillarPoolSize; i++) {
			pillars[i].GetComponent<Rigidbody2D>().velocity = Vector2.zero;
		}
	}
}
